package com.oncallmng.service.impl;

import com.oncallmng.common.PageParam;
import com.oncallmng.common.PageResult;
import com.oncallmng.dao.UserDao;
import com.oncallmng.dto.UserDTO;
import com.oncallmng.exception.BusinessException;
import com.oncallmng.model.User;
import com.oncallmng.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 用户服务实现类
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    
    private final UserDao userDao;
    
    @Override
    public User getById(String id) {
        User user = userDao.findById(id);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        return user;
    }
    
    @Override
    public User getByUsername(String username) {
        return userDao.findByUsername(username);
    }
    
    @Override
    public PageResult<UserDTO> page(PageParam pageParam) {
        // 计算偏移量
        int offset = (pageParam.getPageNum() - 1) * pageParam.getPageSize();
        
        // 查询用户列表
        List<User> users = userDao.findPage(offset, pageParam.getPageSize(), 
                pageParam.getOrderBy(), pageParam.getOrderDirection());
        
        // 查询总数
        long total = userDao.count();
        
        // 转换为DTO
        List<UserDTO> userDTOs = new ArrayList<>();
        for (User user : users) {
            UserDTO userDTO = new UserDTO();
            // 简化处理，实际应使用对象映射工具
            userDTO.setId(user.getId());
            userDTO.setUsername(user.getUsername());
            userDTO.setName(user.getName());
            userDTO.setEmail(user.getEmail());
            userDTO.setPhone(user.getPhone());
            userDTO.setEnabled(user.getEnabled());
            userDTO.setBackupUserId(user.getBackupUserId());
            userDTOs.add(userDTO);
        }
        
        return PageResult.build(userDTOs, total, pageParam);
    }
    
    @Override
    @Transactional
    public String create(User user) {
        // 检查用户名是否已存在
        if (userDao.findByUsername(user.getUsername()) != null) {
            throw new BusinessException("用户名已存在");
        }
        
        // 设置ID
        user.setId(UUID.randomUUID().toString());
        
        // 默认启用
        if (user.getEnabled() == null) {
            user.setEnabled(true);
        }
        
        // 插入用户
        userDao.insert(user);
        
        return user.getId();
    }
    
    @Override
    @Transactional
    public void update(User user) {
        // 检查用户是否存在
        if (userDao.findById(user.getId()) == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 更新用户
        userDao.update(user);
    }
    
    @Override
    @Transactional
    public void delete(String id) {
        // 检查用户是否存在
        if (userDao.findById(id) == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 删除用户
        userDao.delete(id);
    }
    
    @Override
    @Transactional
    public void setEnabled(String id, Boolean enabled) {
        // 检查用户是否存在
        if (userDao.findById(id) == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 设置用户状态
        userDao.setEnabled(id, enabled);
    }
    
    @Override
    @Transactional
    public void setBackupUser(String id, String backupUserId) {
        // 检查用户是否存在
        if (userDao.findById(id) == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 检查备用联系人是否存在
        if (backupUserId != null && userDao.findById(backupUserId) == null) {
            throw new BusinessException("备用联系人不存在");
        }
        
        // 设置备用联系人
        userDao.setBackupUser(id, backupUserId);
    }
    
    @Override
    public List<UserDTO> listByTeamId(String teamId) {
        // 查询团队成员
        List<User> users = userDao.findByTeamId(teamId);
        
        // 转换为DTO
        List<UserDTO> userDTOs = new ArrayList<>();
        for (User user : users) {
            UserDTO userDTO = new UserDTO();
            // 简化处理，实际应使用对象映射工具
            userDTO.setId(user.getId());
            userDTO.setUsername(user.getUsername());
            userDTO.setName(user.getName());
            userDTO.setEmail(user.getEmail());
            userDTO.setPhone(user.getPhone());
            userDTO.setEnabled(user.getEnabled());
            userDTO.setBackupUserId(user.getBackupUserId());
            userDTOs.add(userDTO);
        }
        
        return userDTOs;
    }
} 